package es.aa.android.cartomatic.customview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ScrollView;
import es.aa.android.cartomatic.R;


@SuppressLint("NewApi") 
public class mScrollView extends ScrollView {

    private View stick;
    private View top;
    private int padding = 0;
    private static boolean remove = false;

    public mScrollView(Context context) {
        super(context);
        initView();
    }

    public mScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public mScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        setOverScrollMode(ScrollView.OVER_SCROLL_NEVER);
        setVerticalScroll(false);
        setHorizontalScroll(false);
        initObservers();
    }

    public void setVerticalScroll(boolean verticalScroll) {
        setVerticalScrollBarEnabled(verticalScroll);
        setVerticalFadingEdgeEnabled(verticalScroll);
        setVerticalFadingEdgeEnabled(verticalScroll);
    }

    public void setHorizontalScroll(boolean horizontalScroll) {
        setHorizontalFadingEdgeEnabled(horizontalScroll);
        setHorizontalScrollBarEnabled(horizontalScroll);
        setHorizontalFadingEdgeEnabled(horizontalScroll);
    }

    private void initObservers() {
        getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        onScrollSticky();
                    }
                }
        );

        getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {

                    @Override
                    public void onGlobalLayout() {
                        scrollTo(0, 0);
                        if (remove) {
                            getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        }
                        remove = true;
                    }
                }
        );

    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        onScrollSticky();
    }

    @Override
    public int computeVerticalScrollRange() {
        return super.computeVerticalScrollRange();
    }


    public void onScrollSticky() {
        if (stick != null) {
            int topHeight = 0;
            if (top != null) {
                topHeight = top.getHeight() + getResources().getDimensionPixelOffset(R.dimen.margin_layout);
            }
            stick.setTranslationY(Math.max(padding, topHeight - getScrollY()));
        }
    }

    public void setStickItem(View stick, View top) {
        this.stick = stick;
        this.top = top;
    }

    public void setSwipeLayoutRefreshing(boolean refreshing) {
        if (refreshing) {
            padding = 6;
        } else {
            padding = 0;
        }
    }

}